<template>
  <div>
    <el-button icon="el-icon-back" circle @click="back"></el-button>
    <el-form ref="form" :model="form" status-icon :rules="rules" label-width="120px">
      <el-form-item label="用户编号">
        <el-input v-model="form.uid" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="所属机构编号">
        <el-input v-model="form.jid" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="用户名">
        <el-input v-model="form.username" prop="username"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="form.password" prop="password"></el-input>
      </el-form-item>
      <el-form-item label="真实姓名">
        <el-input v-model="form.truename" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="身份证号">
        <el-input v-model="form.idcardnumber" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="用户权限">
        <el-input v-model="form.authority" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="用户身份">
        <el-input v-model="form.identity" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="upd">更新</el-button>
        <el-button @click="cancel">恢复</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "EditUser",
  uid:'',
  data(){
    return{
      usernametemp:'',
      passwordtemp:'',
      temp:[],
      form: {
        uid: '',
        jid: '',
        username: '',
        password: '',
        truename:'',
        idcardnumber: '',
        authority: '',
        identity: ''
      },
      rules: {
        username: [
          {required: true, message: '用户名不可为空', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '密码不可为空', trigger: 'blur'}
        ]
      },
    }
  },
  created: function() {
    this.uid=this.$route.params.uid;
  },
  beforeRouteEnter: (to,from,next) => {
    console.log('进入路由之前');//加载数据
    next(vm => {
      vm.selectinfo();
    })
  },
  methods:{
    back: function (){
      this.$router.push('/func/profile');
    },
    selectinfo: function() {
      this.axios({
        method:"post",
        headers: { 'content-type': 'application/json' },
        url: "http://evidence.frogking.cn/userController/selectByUid",
        params:{
          token:localStorage.getItem("token_only"),
        }
      }).then(res=>{
        if(res["data"]["code"]==='206'){
          this.form=res["data"]["item"];
          this.temp=this.info
          this.usernametemp=res["data"]["item"]["username"];
          this.passwordtemp=res["data"]["item"]["password"];
        }
        else if(res["data"]["code"]==='210'){
          this.$alert('没有该用户', '错误提示', {
            confirmButtonText: '确定'
          });
        }
        else if(res["data"]["code"]==='000'){
          this.$alert('token失效', '错误提示', {
            confirmButtonText: '确定',
            callback: action => {
              this.$router.push("/login");
              localStorage.removeItem("token_only");
            }
          });
        }
        else if(res["data"]["code"]==='001'){
          this.$alert('身份不满足，禁止操作', '错误提示', {
            confirmButtonText: '确定'
          });
        }
        else if(res["data"]["code"]==='002'){
          this.$alert('权限不满足，禁止操作', '错误提示', {
            confirmButtonText: '确定'
          });
        }
      }).catch(function (error) {
        console.log(error);
      });
    },
    upd(){
      if(this.usernametemp===this.form.username&&this.passwordtemp===this.form.password){
        this.$alert('您未修改任何信息。', '提示', {
          confirmButtonText: '确定',
          callback: action => {
            this.$router.push('/personalEdit')
          }
        });
      }
      else if(this.usernametemp===this.form.username&&this.passwordtemp!==this.form.password){
        this.$confirm('确定修改密码？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.updforsure();
        }).catch(() => {
          this.$router.push('/personalEdit')
        });
      }
      else if(this.usernametemp!==this.form.username&&this.passwordtemp!==this.form.password){
        this.$confirm('确定修改用户名和密码？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.updforsure();
        }).catch(() => {
          this.$router.push('/personalEdit')
        });
      }
      else if(this.usernametemp!==this.form.username&&this.passwordtemp===this.form.password){
        this.updforsure();
      }
    },
    updforsure:function(){
      this.axios({
        method:"post",
        headers: { 'content-type': 'application/json' },
        url: "http://evidence.frogking.cn/userController/update",
        params:{
          token:localStorage.getItem("token_only"),
          jid: this.form.jid,
          username: this.form.username,
          password : this.form.password,
          truename: this.form.truename,
          idcardNumber: this.form.idcardnumber,
          authority : this.form.authority,
          identity:this.form.identity
        }
      }).then(res=>{
        console.log(res)
        if(res["data"]["code"]==='200'){
          this.$router.push('/login')
        }
        else if(res["data"]["code"]==='216'){
          this.$router.push('/func/profile')
        }
        else if(res["data"]["code"]==='201'){
          this.$alert('系统错误！更新异常！', '错误提示', {
            confirmButtonText: '确定',
            callback: action => {
              this.$router.push('/personalEdit');
            }
          });
        }
        else if(res["data"]["code"]==='214'){
          this.$alert('用户名重复！更新异常！', '错误提示', {
            confirmButtonText: '确定',
            callback: action => {
              this.$router.push('/personalEdit');
            }
          });
        }
        else if(res["data"]["code"]==='215'){
          this.$alert('修改了不可修改的参数值！更新异常！', '错误提示', {
            confirmButtonText: '确定',
            callback: action => {
              this.$router.push('/personalEdit');
            }
          });
        }
        else if(res["data"]["code"]==='210'){
          this.$alert('用户不存在！更新异常！', '错误提示', {
            confirmButtonText: '确定',
            callback: action => {
              this.$router.push('/personalEdit');
            }
          });
        }
        else if(res["data"]["code"]==='000'){
          this.$alert('token失效', '错误提示', {
            confirmButtonText: '确定',
            callback: action => {
              this.$router.push("/login");
              localStorage.removeItem("token_only");
            }
          });
        }
        else if(res["data"]["code"]==='001'){
          this.$alert('token权限不满足，禁止操作', '错误提示', {
            confirmButtonText: '确定'
          });
        }
        else if(res["data"]["code"]==='002'){
          this.$alert('token权限不满足，禁止操作', '错误提示', {
            confirmButtonText: '确定'
          });
        }
      }).catch(function (error) {
        console.log(error);
      });
    },
    cancel:function (){
      this.selectinfo();
    }
  }

}
</script>
<style scoped>

</style>
